
package com.funo.government.info.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.preference.PreferenceManager;

import com.funo.government.info.components.PullService;
import com.funo.government.info.constant.Constant;

/**
 * 通用工具类
 */
public class Utils {

    public static boolean hasFroyo() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    public static boolean hasGingerbread() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean hasHoneycombMR1() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
    }

    public static boolean hasJellyBean() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
    }

    /**
     * 获取bitmap的size大小.
     * 
     * @param bitmap
     * @return size in bytes
     */
    @SuppressLint("NewApi")
    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            return bitmap.getByteCount();
        }
        // Pre HC-MR1
        return bitmap.getRowBytes() * bitmap.getHeight();
    }

    /**
     * 获取内存大小
     * 
     * @param context
     * @return
     */
    public static int getMemoryClass(Context context) {
        return ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getMemoryClass();
    }

    /**
     * 是否支持actionbar
     * 
     * @return
     */
    public static boolean hasActionBar() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    /**
     * 处理实时推送服务
     * 
     * @param context
     */
    public static void ProcessPullService(Context context) {
        boolean isPush = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
                Constant.PRE_KEY_PUSH_SETTINGS, true);

        if (isPush) {
            Intent pullService = new Intent(context, PullService.class);
            context.startService(pullService);
        } else {
            Intent pullService = new Intent(context, PullService.class);
            context.stopService(pullService);
        }
    }

    /**
     * 从InputStream中读取数据并转为字符串类型
     * 
     * @param is
     * @return
     * @throws IOException
     */
    public static String convertToString(InputStream is) throws IOException {
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String buff;
            while ((buff = reader.readLine()) != null) {
                builder.append(buff);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        return builder.toString();
    }

    private Utils() {
    };
}
